package com.bobi.tfca.ui.mainnavigation.home.activity

import android.view.View
import android.widget.ImageView
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.bobi.tfca.network.ApiResponse
import com.bobi.tfca.network.api.ActivityAPI
import com.bobi.tfca.network.response.ActivityResponse
import com.bobi.tfca.ui.mainnavigation.user.activity.MyActivityFragment
import com.bobi.tfca.util.LogUtil
import com.bobi.tfca.viewmodel.PublicViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

class ActivityFragmentVM : ViewModel() {

    val activities by lazy {
        MutableLiveData(ArrayList<ActivityResponse>())
    }

    fun findAllActivities(
        publicViewModel: PublicViewModel,
        activityFragment: ActivityFragment,
        noDataIV: ImageView
    ) {
        publicViewModel.apply {
            request(ActivityAPI::class.java).findAll().getResponse {
                it.collect {
                    when (it) {
                        is ApiResponse.Error -> {
                            LogUtil.e(it.errMsg)
                        }

                        ApiResponse.Loading -> LogUtil.e("Loading")
                        is ApiResponse.Success -> {
                            withContext(Dispatchers.Main) {
                                val list = it.data?.data
                                if (list != null) {
                                    if (list.size == 0) {
                                        noDataIV.visibility = View.VISIBLE
                                    } else {
                                        noDataIV.visibility = View.GONE
                                    }
                                    activities.value = list
                                } else {
                                    // 如果没有数据，设置背景图片
                                    noDataIV.visibility = View.VISIBLE
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    fun findMyActivities(
        publicViewModel: PublicViewModel,
        myActivityFragment: MyActivityFragment,
        noDataIV: ImageView
    ) {
        publicViewModel.apply {
            request(ActivityAPI::class.java).findMyActivities().getResponse {
                it.collect {
                    when (it) {
                        is ApiResponse.Error -> {
                            LogUtil.e(it.errMsg)
                        }

                        ApiResponse.Loading -> LogUtil.e("Loading")
                        is ApiResponse.Success -> {
                            withContext(Dispatchers.Main) {
                                val list = it.data?.data
                                if (list != null) {
                                    if (list.size == 0) {
                                        noDataIV.visibility = View.VISIBLE
                                    } else {
                                        noDataIV.visibility = View.GONE
                                    }
                                    activities.value = list
                                } else {
                                    // 如果没有数据，设置背景图片
                                    noDataIV.visibility = View.VISIBLE
                                }
                            }
                        }
                    }
                }
            }
        }
    }

}